﻿using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace _411Client.Connect
{
    class Serialize
    {  
        /// <summary>
        /// 序列化
        /// </summary>
        public static byte[] DoSerialize(object obj)
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            formatter.Serialize(ms, obj);
            byte[] tmp = ms.ToArray();
            ms.Close();
            return tmp;
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        public static object Userialize(Byte[] bytes)
        {
            MemoryStream pMemoryStream = new MemoryStream(bytes);//创建内存流对象
             pMemoryStream.Seek(0, SeekOrigin.Begin);//调整内存流指针位置
             IFormatter formatter = new BinaryFormatter();
            formatter.Binder = new UBinder();
            object pInstance = formatter.Deserialize(pMemoryStream);//从内存流中反序列化得到object对象
            return pInstance;
        }
    }
}
